#include <stdio.h>
#include <stdlib.h>

#include "datatypes.h"

void free_concept(Concept* c) {
	int i;
	switch (c->type) {
		case ATOMIC_CONCEPT:
			// atomic concepts are reused, shouldn't be freed!
			// free(c);
			break;
		case EXISTENTIAL_RESTRICTION:
			// there can be only atomic roles in exist restrictions
			// atomic roles are reused, shouldn't be freed!
			// free(c->description.exists->role);
			free_concept(c->description.exists->filler);
			free(c->description.exists);
			free(c);
			break;
		case CONJUNCTION:
			for (i = 0; i < c->description.conj->count; ++i) {
				free_concept(c->description.conj->conjuncts[i]);
			}
			free(c->description.conj);
			free(c);
			break;
		default:
			fprintf(stderr,"unknown concept type, aborting\n");
			exit(-1);
	}

}

void free_conjunction(Conjunction* c) {
	int i;
	for (i = 0; i < c->count; ++i) {
		free_concept(c->conjuncts[i]);
	}
	free(c);
}

void free_eqclass_axiom(Axiom* ax) {
	free_concept(ax->body.subclass_ax->lhs);
	free_concept(ax->body.subclass_ax->rhs);
	free(ax);
}

void free_subclass_axiom(Axiom* ax) {
	free_concept(ax->body.eqclass_ax->lhs);
	free_concept(ax->body.eqclass_ax->rhs);
	free(ax);
}

void free_axiom(Axiom* ax) {
	switch (ax->type) {
		case SUBCLASS_AX:
			free_eqclass_axiom(ax);
			break;
		case EQCLASS_AX:
			free_subclass_axiom(ax);
			break;
		case SUBROLE_AX:
			// TODO
			break;
		default:
			fprintf(stderr, "unknown axiom type, aborting\n");
			exit(-1);
	}
}

